﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.fjyc.Integration.HR.Interface;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;

namespace HR.Ldap.Imp
{
    public class LDAPSync : IEmployeeOperation
    {
        private readonly static string ConnStr = ConfigurationManager.ConnectionStrings["MysqlConStr"].ConnectionString;

        private MySqlConnection conn = null;

        public LDAPSync()
        {
            conn = new MySqlConnection(ConnStr);
            conn.Open();
        }

        public object Server
        {
            get { throw new NotImplementedException(); }
        }

        public void PushEmployee(Employee employee)
        {
            if (string.IsNullOrEmpty(employee.O_UID))
                return;
            //check if should delete person
            if (employee.STATUSCODE != "1")
            {
                DelEmp(conn, employee);
            }
            else
            {
                //check if the person is exists
                MySqlCommand command2 = new MySqlCommand();
                command2.Connection = conn;
                command2.CommandType = CommandType.Text;
                command2.CommandText = @"select i.orgcode from ldap_entries l2
inner join institutes i on l2.keyval = i.id
where (l2.oc_map_id=3 or l2.oc_map_id=5)
and l2.id =
(
select parent  from ldap_entries l1
    inner join persons p on l1.keyval = p.id
    where l1.oc_map_id=1 and p.surname='" + employee.PERSONCODE + "' limit 1)";//sql clauses
                object emp = command2.ExecuteScalar();

                //已存在该人
                if (emp != null)
                {
                    //如果组织也相同则跳过
                    if (emp.ToString() == employee.DEPTCODE) return;
                    else//如果组织不同，则先删除再新增
                    {
                        DelEmp(conn,employee);
                        AddEmp(conn, employee);

                    }

                }
                //不存在该人
                else
                {
                    AddEmp(conn, employee);
                }
            }

        }

        private void AddEmp(MySqlConnection conn, Employee employee)
        {
            MySqlCommand command = new MySqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "create_person";//name of procdure
            command.Parameters.Add(new MySqlParameter("personCN", employee.O_UID));
            command.Parameters.Add(new MySqlParameter("personCode", employee.PERSONCODE));
            command.Parameters.Add(new MySqlParameter("personuid", employee.O_UID));
            command.Parameters.Add(new MySqlParameter("personmail", null));
            command.Parameters.Add(new MySqlParameter("parentCode", employee.DEPTCODE));
            command.Parameters.Add(new MySqlParameter("personPwd", employee.O_UID));//pwd maybe encrypted
            MySqlParameter param = new MySqlParameter("@entryid", employee.PERSONID);
            param.Direction = ParameterDirection.Output;
            command.Parameters.Add(param);
            command.ExecuteNonQuery();
        }

        private void DelEmp(MySqlConnection conn, Employee employee)
        {
            MySqlCommand command = new MySqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "delete_person";//name of procdure
            command.Parameters.Add(new MySqlParameter("personCode", employee.PERSONCODE));
            command.ExecuteNonQuery();
        }

        public void Dispose()
        {
            if (conn != null)
                conn.Close();
        }


        public DateTime GetStartDate()
        {
            throw new NotImplementedException();
        }

        public object PushEndDate(DateTimePeroid dt)
        {
            throw new NotImplementedException();
        }
    }
}
